package com.tingfeng.xiuzhen;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.tencent.connect.auth.AuthAgent;
import com.tencent.connect.auth.QQToken;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Field;

/**
 * Created by zhangtao on 2018/5/21.
 */

public class MyApplication extends Application {
    /**
     * 系统上下文
     */
    private static Context mAppContext;

    public static Tencent mTencent;
    public static IWXAPI wxApi;

    // 创建一个Handler
    public static Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    // 网络请求错误统一处理
//                    Toast.makeText(getAppContext(), GlobalData.NET_FAIL_MSG, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }

    };

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();

        initQQ();
        initWeiXin();
    }

    // 初始化微信
    private void initWeiXin() {
        // 通过WXAPIFactory工厂,获取IWxApi实例
        wxApi = WXAPIFactory.createWXAPI(MyApplication.getAppContext(), GlobalData.WX_APP_ID, true);
        // 将应用的AppID注册到微信
        wxApi.registerApp(GlobalData.WX_APP_ID);
    }

    // 初始化QQ
    private void initQQ() {
        // Tencent类是SDK的主要实现类，开发者可通过Tencent类访问腾讯开放的OpenAPI。
        mTencent = Tencent.createInstance(GlobalData.QQ_APP_ID, mAppContext);
        // QQ客户端和网页端登录处理
        fixWebAuth(this, mTencent);
    }

    /**
     * 解决网页授权问题
     *
     * @return true: 成功; false: 失败
     */
    private boolean fixWebAuth(Context context, Tencent tencent) {

        if (mTencent.isQQInstalled(context)) {
            return true;
        }
        if (tencent == null) {
            return false;
        }
        boolean ok = false;
        try {
            Field aField = tencent.getClass().getDeclaredField("a");
            aField.setAccessible(true);
            Object cVal = aField.get(tencent);
            Field authAgentField = cVal.getClass().getDeclaredField("a");
            authAgentField.setAccessible(true);
            authAgentField.set(cVal, new FixWebAuthAgent(tencent.getQQToken()));
            ok = true;
        } catch (Throwable ignore) {
        }
        return ok;
    }

    /**
     * 解决网页授权问题的AuthAgent
     */
    private static class FixWebAuthAgent extends AuthAgent {

        FixWebAuthAgent(QQToken qqToken) {
            super(qqToken);
        }

        @Override
        protected Bundle a() {
            Bundle bundle = super.a();
            if (bundle != null) {
                // 主要原因在这, 腾讯的server估计是判断这个版本号来进行限制登录的, 从1.5版本后不能打开网页授权了
                bundle.putString("sdkv", "1.4.0");
            }
            return bundle;
        }
    }

    /**
     * 获取系统上下文：用于ToastUtil类
     */
    public static Context getAppContext() {
        return mAppContext;
    }

}
